package com.coocaa.launcher.framework.manager.setting;

import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import android.view.Display;
import android.view.WindowManager;

import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.utils.LogUtils;
import com.skyworth.framework.skysdk.properties.SkySystemProperties;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class DeviceInfo {
    public static String getManufactory() {
        return android.os.Build.MANUFACTURER;
    }

    public static int sdkVersion() {
        return android.os.Build.VERSION.SDK_INT;
    }

    public static String deviceModel() {
        return android.os.Build.MODEL;
    }

    public static String getMAC() {
        return CoocaaApplication.getMAC();
    }

    public static String getPIX() {
        String is4K = SkySystemProperties.getProperty("third.get.4k");
        if (is4K != null && is4K.trim().contains("1"))
            return 3840 + "*" + 2160;
        Display display = ((WindowManager) CoocaaApplication.getContext().getSystemService(
                Context.WINDOW_SERVICE)).getDefaultDisplay();
        int W = display.getWidth();
        int H = display.getHeight();
        return String.valueOf(W) + "*" + String.valueOf(H);
    }

    /**
     * 获取设备内存大小值
     *
     * @return 内存大小, 单位MB
     */
    public static long getTotalMemory() {
        String str1 = "/proc/meminfo";
        String str2;
        String[] arrayOfString;
        long initial_memory = 0;
        try {
            FileReader localFileReader = new FileReader(str1);
            BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);
            str2 = localBufferedReader.readLine();
            if (str2 != null) {
                LogUtils.d("info", "str2:" + str2);
                int index = str2.lastIndexOf("kB");
                String str3 = str2.substring(0, index).trim();
                int index2 = str3.lastIndexOf(":");
                String str4 = str3.substring(index2 + 1).trim();
                LogUtils.d("info", "str4:" + str4);
                initial_memory = Long.parseLong(str4) / 1024;
                LogUtils.d("info", "initial_memory:" + initial_memory + "M");
            }
            localBufferedReader.close();
            return initial_memory;
        } catch (IOException e) {
            return -1;
        }
    }

    public static String getSystemAvaialbeMemorySize() {
        ActivityManager mActivityManager = (ActivityManager) CoocaaApplication.getContext()
                .getSystemService(Context.ACTIVITY_SERVICE);
        // 获得MemoryInfo对象
        MemoryInfo memoryInfo = new MemoryInfo();
        // 获得系统可用内存，保存在MemoryInfo对象上
        mActivityManager.getMemoryInfo(memoryInfo);
        long memSize = memoryInfo.availMem;
        return String.valueOf(memSize / (1024 * 1024));
    }

    // 获取系统的总内存
    public static float getTotalInternalMemorySize() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long totalBlocks = stat.getBlockCount();
        float size = (float) (totalBlocks * blockSize) / (1024 * 1024 * 1024);
        size = fixFloat(size);
        return size;
    }

    // 获取系统可用的总内存
    public static float getAvailableInternalMemorySize() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        float size = (float) (availableBlocks * blockSize) / (1024 * 1024 * 1024);
        size = fixFloat(size);
        return size;
    }

    public static float fixFloat(float f) {
        BigDecimal b = new BigDecimal(f);
        float f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
        return f1;
    }

    // 大部分时间不准，暂时不用
    // public static String getIPInfo()
    // {
    // //获取wifi服务
    // WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
    // //判断wifi是否开启
    // if (!wifiManager.isWifiEnabled()) {
    // LogUtils.d("info", "wifi is not enabled");
    // wifiManager.setWifiEnabled(true);
    // }
    // WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    // int ipAddress = wifiInfo.getIpAddress();
    // LogUtils.d("info", "wifi ipAddress:"+ipAddress);
    // String ip = intToIp(ipAddress);
    // return ip;
    // }
    //
    // private static String intToIp(int i) {
    //
    // return (i & 0xFF ) + "." +
    // ((i >> 8 ) & 0xFF) + "." +
    // ((i >> 16 ) & 0xFF) + "." +
    // ( i >> 24 & 0xFF) ;
    // }

    public static String getIPInfo() {
        String ipAddress = "0.0.0.0";
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                    .hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr
                        .hasMoreElements(); ) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()) {
                        ipAddress = inetAddress.getHostAddress().toString();
                    }
                }
            }
        } catch (SocketException ex) {
        }
        return ipAddress;
    }

}
